package com.qf.dao;


import com.qf.entity.Account;
import com.qf.utils.DruidUtil;
import org.apache.commons.dbutils.DbUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/*
杨威
2021/9/6
19:52
*/
public class AccountDaoImpl {
    QueryRunner queryRunner = new QueryRunner();

    public Account select(String cardNum) {
        Account query = null;
        try {
            String sql = "select * from account where cardnum=?";
            Connection connection = DruidUtil.getConnection();
            System.out.println("connection = " + connection);
            query = queryRunner.query(connection, sql, new BeanHandler<>(Account.class), cardNum);
            System.out.println(connection);
            return query;
        } catch (SQLException e) {
            throw new RuntimeException(e.getMessage()+"根据卡号查询用户出现异常");
        }

    }

    public int update(Account account) {
        try {
            String sql="update account set balance=? where cardnum=?";
            Connection connection = DruidUtil.getConnection();
            System.out.println("connection = " + connection);
            int update = queryRunner.update(connection, sql, account.getBalance(), account.getCardNum());
            return update;
        } catch (SQLException e) {
            throw new RuntimeException(e.getMessage()+"修改出现异常");
        }
    }
}
